﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Configuration;
using System.ServiceModel;
using PSE.Hosting.ServiceHoster.Business;
using PSE.Hosting.ServiceHoster.Logging;
using PSE.Hosting.ServiceHoster.Configuration;
using System.IO;
using System.Xml;
using System.Security.Policy;
using System.Reflection;
using PSE.Hosting.ServiceHoster.Business.Processes;
using PSE.Framework.Process;
using PSE.Framework.Configuration;

namespace PSE.Hosting.ServiceHoster.Process
{
    class Program : BaseProgram
    {
        static Program()
        {
            Initialize(PSE.Framework.Configuration.Components.SystemComponents.System, PSE.Framework.Configuration.Components.SystemComponents.HosterService);
        }

        static int Main(string[] args)
        {
            string packageName;

            try
            {
                if (args.Length != 8)
                    throw new ArgumentException("Invalid number of arguments");

                if (string.IsNullOrEmpty(args[0]))
                    throw new ArgumentException("Package Name cannot be null");

                packageName = args[0];

                if (!ConfigurationFile.Instance.GetPackages().Any(p => p.Name == packageName))
                    throw new ArgumentException(string.Format("Invalid Package Name: '{0}'", packageName));

                // Titulo do processo
                Console.Title = packageName;

                using (ServiceHosterPackageProcessManager process = ServiceHosterPackageProcessManager.GetInstance())
                {
                    process.Initialize(packageName, new ParentProcessStartInfo(args));

                    process.WaitForExit();
                }

                return 0;
            }
            catch (Exception ex)
            {
                Logger.Log(ex, Logger.EntryType.Error);
                return 1;
            }
        }
    }
}
